import os
import sys
import pygame
from pygame.locals import *
import pygame.surfarray
from math import sqrt, floor
import random
#import psyco
import sane as psyco

yellow = (230,230,0)  
red = (255,0,0)  
green = (0,255,0)  
blue = (0,0,255)
ocean = (0,40,150)
orange = (240,90,15)
purple = (150,0,150)
dark_purple = (90,0,120)
violet = (150,30,150)
black = (0,0,0) 
white = (255,255,255)
dark_grey = (80,80,80)
light_grey = (180,180,180)
very_light_grey = (210,210,210)
brown = (185,135,10)
almostblack = (1,1,1)
almostwhite = (254,254,254)
cyan = (150,255,255)
muddy = (140,150,0)

def color_to_rgb(color):
    # takes e.g. 0x0AA6622 and returns (170,102,34)
    r = color / (256*256)
    color = color-r*256*256
    g = color/256
    color = color-g*256
    b = color
    return (r, g, b)

def rgb_to_color(rgb):
    # takes e.g. (170,102,34) and returns a number equalling 0x0AA6622
    return b + 256*g + 256*256*r
  
def load_image(file):
    file = os.path.join('data', file)
    surface = pygame.image.load(file)
    return surface.convert()

def get_tile(tileset,iconsize,pic_x,pic_y,colorkey):
    subpic = pygame.Surface((iconsize,iconsize))
    subpic.blit(tileset,(0,0),((iconsize*pic_x,iconsize*pic_y),(iconsize*(pic_x+1),iconsize*(pic_y+1))))
    subpic.set_colorkey(colorkey)
    return subpic

def scale_up_by(image,x):
    # x should be a whole number
    #result = pygame.Surface((image.get_width()*x,image.get_height()*x))
    #result.set_colorkey(image.get_colorkey())
    #for i in range(image.get_width()):
        #for j in range(image.get_height()):
            #val = image.get_at((i,j))
            #for ii in range(x):
                #iii = i*x+ii
                #for jj in range(x):
                    #jjj = j*x+jj
                    #result.set_at((iii,jjj),val)
    #return result
    return pygame.transform.scale(image,(image.get_width()*x,image.get_width()*x))
        
def colorchange(surf,col_from,col_to):
    original_col_from = col_from
    original_col_to = col_to
    new_surf = pygame.Surface((surf.get_width(),surf.get_height()))
    change_lightness = float(sum(col_to))/float(sum(col_from))
    avg_from = sum(col_from)/3 
    col_from = (col_from[0]-avg_from,col_from[1]-avg_from,col_from[2]-avg_from)
    ssq_from_rgb = col_from[0]*col_from[0]+col_from[1]*col_from[1]+col_from[2]*col_from[2]
    if sqrt(ssq_from_rgb)==0:
        col_from = (0,0,0)
    else:
        col_from = (col_from[0]/sqrt(ssq_from_rgb),col_from[1]/sqrt(ssq_from_rgb),col_from[2]/sqrt(ssq_from_rgb))
    avg_to = sum(col_to)/3 
    col_to = (col_to[0]-avg_to,col_to[1]-avg_to,col_to[2]-avg_to)
    ssq_to_rgb = col_to[0]*col_to[0]+col_to[1]*col_to[1]+col_to[2]*col_to[2]
    if sqrt(ssq_to_rgb)==0:
        col_to = (0,0,0)
    else:
        col_to = (col_to[0]/sqrt(ssq_to_rgb),col_to[1]/sqrt(ssq_to_rgb),col_to[2]/sqrt(ssq_to_rgb))
    for i in range(surf.get_width()):
        for j in range(surf.get_width()):
            current_col = surf.get_at((i,j))[:3]
            from_comp = current_col[0]*col_from[0] + current_col[1]*col_from[1] + current_col[2]*col_from[2]
            if from_comp>80:
                current_col = (change_lightness*(current_col[0]+from_comp*(col_to[0]-col_from[0])),change_lightness*(current_col[1]+from_comp*(col_to[1]-col_from[1])),change_lightness*(current_col[2]+from_comp*(col_to[2]-col_from[2])))
                current_col = (floor(min(255,max(0,current_col[0]))),floor(min(255,max(0,current_col[1]))),floor(min(255,max(0,current_col[2]))))
            elif col_from[0]==0 and col_from[1]==0 and col_from[2]==0:
                if max(abs(current_col[0]-original_col_from[0]),abs(current_col[1]-original_col_from[1]),abs(current_col[2]-original_col_from[2])) < 60:
                    current_col = (original_col_to[0]-original_col_from[0]+current_col[0],original_col_to[1]-original_col_from[1]+current_col[1],original_col_to[2]-original_col_from[2]+current_col[2])
                    current_col = (floor(min(255,max(0,current_col[0]))),floor(min(255,max(0,current_col[1]))),floor(min(255,max(0,current_col[2]))))
            current_col = (current_col[0],current_col[1],current_col[2],255)
            new_surf.set_at((i,j),current_col)
    new_surf.set_colorkey(surf.get_colorkey())
    return new_surf

def darken(surf,by,except_col=black):
    new_surf = pygame.Surface((surf.get_width(),surf.get_height()))
    for i in range(surf.get_width()):
        for j in range(surf.get_width()):
            current_col = surf.get_at((i,j))[:3]
            if current_col <> except_col:
                current_col = (min(255,max(0,floor(current_col[0]*(1-by)))),min(255,max(0,floor(current_col[1]*(1-by)))),min(255,max(0,floor(current_col[2]*(1-by)))),255)
            new_surf.set_at((i,j),current_col)
    new_surf.set_colorkey(surf.get_colorkey())
    return new_surf    

def lighten(surf,by,only_col=None,except_col=None):
    new_surf = pygame.Surface((surf.get_width(),surf.get_height()))
    for i in range(surf.get_width()):
        for j in range(surf.get_width()):
            current_col = surf.get_at((i,j))[:3]
            if except_col is None or current_col <> except_col:
                if only_col is None or current_col==only_col:
                    current_col = (min(255,max(0,floor(255-(255-current_col[0])*(1-by)))),min(255,max(0,floor(255-(255-current_col[1])*(1-by)))),min(255,max(0,floor(255-(255-current_col[2])*(1-by)))),255)
            new_surf.set_at((i,j),current_col)
    new_surf.set_colorkey(surf.get_colorkey())
    return new_surf  

def partialize(surf,prop,background_col=black):
    new_surf = pygame.Surface((surf.get_width(),surf.get_height()))
    for i in range(surf.get_width()):
        for j in range(surf.get_width()):
            current_col = surf.get_at((i,j))[:3]
            if random.random()<prop:
                new_surf.set_at((i,j),current_col)
            else:
                new_surf.set_at((i,j),background_col)
    new_surf.set_colorkey(surf.get_colorkey())
    return new_surf      

def greyout(surf):
    new_surf = pygame.Surface((surf.get_width(),surf.get_height()))
    for i in range(surf.get_width()):
        for j in range(surf.get_width()):
            current_col = surf.get_at((i,j))[:3]
            if current_col <> surf.get_colorkey():
                new_grey = sum(current_col)/3
                current_col = (new_grey,new_grey,new_grey,255)
                new_surf.set_at((i,j),current_col)
    new_surf.set_colorkey(surf.get_colorkey())
    return new_surf    

def null_image():
    return pygame.Surface((1,1))

# do some psyco bindings 
psyco.bind(darken)
psyco.bind(lighten)
psyco.bind(partialize)
psyco.bind(colorchange)
psyco.bind(greyout)
psyco.bind(get_tile)
        
        